package com.listener;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.yinhai.activity.ChatViewImgActivity;
import com.yinhai.activity.OtherPosLocation;
import com.yinhai.constant.Contact;
import com.yinhai.model.ChatMsgEntity;
import com.yinhai.model.MsgModel;
import com.yinhai.photo.utils.Photo;
import com.yinhai.service.MsgServiceImpl;
import com.yinhai.util.NullUtils;

public class MsgTextListener implements View.OnClickListener {
    private MsgServiceImpl msgServiceImpl;
    
    private String type;
    
    private MsgModel msgModel;
    
    private ChatMsgEntity entity;
    
    private Context cxt;
    private MediaPlayer mediaPlayer;
    
    public MsgTextListener(String type, MsgModel msgModel, ChatMsgEntity entity, Context cxt) {
        this.type = type;
        this.msgModel = msgModel;
        this.entity = entity;
        this.cxt = cxt;
    }
    
    @Override
    public void onClick(View arg0) {
        // 初始化itemview弹出框
        // LayoutInflater inflater = (LayoutInflater)
        // cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // TODO Auto-generated method stub
        if ((type != null) && (!"".equals(type))) {
            if (Contact.TEXT == Integer.valueOf(type)) {
                // 当内容是文本时候
            } else if (Contact.REDIO == Integer.valueOf(type)) {
                if (NullUtils.isEmpty(msgModel.getContent()) && NullUtils.isEmpty(msgModel.getFileName())) {
                    return;
                }
                // 当内容是语音时
                if (mediaPlayer!=null&&mediaPlayer.isPlaying()) {
                    mediaPlayer.reset();
                    mediaPlayer.stop();
                }
                mediaPlayer=new MediaPlayer();
                mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    
                    @Override
                    public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
                        return false;
                    }
                });
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    
                    @Override
                    public void onCompletion(MediaPlayer arg0) {
                        Log.e("===", "-==");
                    }
                });
                mediaPlayer.setVolume(1, 1);
                try {
                    if (NullUtils.isNotEmpty(msgModel.getContent())) {
                        File file = new File(msgModel.getContent());
                        if (file.exists()) {
                            mediaPlayer.setDataSource(msgModel.getContent());
                        } else {
                            mediaPlayer.setDataSource(msgModel.getFileName());
                        }
                    } else {
                        mediaPlayer.setDataSource(msgModel.getFileName());
                    }
                    
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (Contact.PHOTO == Integer.valueOf(type)) {
                // 当发送的内容是图片时
                msgServiceImpl = new MsgServiceImpl(cxt);
                MsgModel msgModel = msgServiceImpl.getMsgModelByUUID(entity.getUuid());
                File f = new File(Photo.getSaveIMPicPath(), msgModel.getUuid() + ".png");
                if (f.exists()) {
                    Intent it = new Intent(cxt, ChatViewImgActivity.class);
                    it.putExtra("pic", Photo.getSaveIMPicPath() + msgModel.getUuid() + ".png");
                    cxt.startActivity(it);
                } else if (NullUtils.isNotEmpty(msgModel.getFileName())) {
                    Intent it = new Intent(cxt, ChatViewImgActivity.class);
                    it.putExtra("fileName", msgModel.getFileName());
                    cxt.startActivity(it);
                } else {
                    Toast.makeText(cxt, "您还未获取到对方发送图片", Toast.LENGTH_SHORT).show();
                }
                
            } else if (Contact.POSMAP == Integer.valueOf(type)) {
                // 当发送的内容是位置信息
                msgServiceImpl = new MsgServiceImpl(cxt);
                MsgModel msgModel = msgServiceImpl.getMsgModelByUUID(entity.getUuid());
                Intent it = new Intent(cxt, OtherPosLocation.class);
                it.putExtra("name", msgModel.getContent());
                it.putExtra("latitude", msgModel.getLatitude().toString());
                it.putExtra("longitude", msgModel.getLongitude().toString());
                cxt.startActivity(it);
            }
        }
        
    }
    
}
